﻿using System;
using System.IO;

namespace Loader
{
	class Program
	{
		private const string APPLICATION_EXECUTABLE = "GUI.exe";
		private const string APPLICATION_CONFIGURATION = "GUI.exe.config";
		private const string APPLICATION_NAME = "ObjectShelve";

		[LoaderOptimization(LoaderOptimization.MultiDomainHost)]
		[STAThread]
		static void Main(string[] args)
		{
			// Loader and LTemperaturos must reside in the same folder
#if DEBUG
			string statupPath = @"D:\Documents\Software Development\Projects\Object Shelve\GUI\bin\Debug";
#else
            string statupPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
#endif

			// this is where shadow copy will put all files
			string cachePath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
			// application config and executable absolute paths
			string configPath = Path.Combine(statupPath, APPLICATION_CONFIGURATION);
			string assemblyPath = Path.Combine(statupPath, APPLICATION_EXECUTABLE);

			// config shadow copy
			AppDomainSetup setup = new AppDomainSetup
			                       {
			                       	ApplicationName = APPLICATION_NAME,
			                       	ShadowCopyFiles = "true",
			                       	CachePath = cachePath,
			                       	ConfigurationFile = configPath
			                       };

			// create application domain
			AppDomain domain = AppDomain.CreateDomain(APPLICATION_NAME, AppDomain.CurrentDomain.Evidence, setup);

			// start shadow copied application
			domain.ExecuteAssembly(assemblyPath);

			// after the application has finished, clean up
			AppDomain.Unload(domain);
			Directory.Delete(cachePath, true);
		}
	}
}
